package houseRent.View;
import houseRent.Domain.House;
import houseRent.Service.HouseService;
import java.util.Scanner;
/*
1.显示界面
2.接收用户输入
3.调用HouseService 显示菜单
 */
public class HouseView {
    //显示主菜单
    Scanner scanner=new Scanner(System.in);
    private boolean loop=true;
    private String key;
    private HouseService houseService=new HouseService(3);//给我的数据库说有多大的空间
    public void listHouse(){
        System.out.println("=================房屋信息===============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        //遍历数组的操作处理
        House[]houses=houseService.List();//定义一个数组来接收这个数据的操作处理实现
        for(int i=0;i<houses.length;i++){//但是具体有几个数据如果后面没有初始化的化会变成空指针
            if(houses[i]==null){
                break;
            }
            System.out.println(houses[i].toString());
        }
        System.out.println("房屋列表信息显示完毕");
}
//写出来 谁去实现呢 那就是服务层去干的事情 这个就相当于是前端 进行接收用户的输入的数据
public void addHouse(){
    System.out.println("========添加房屋信息=========");
    System.out.println("请输入姓名：");
    String name = scanner.nextLine();
    System.out.println("请输入电话");
    String phone = scanner.nextLine();
    System.out.println("请输入地址:");
    String address = scanner.nextLine();
    System.out.println("请输入每日的租金:");
    int rent = scanner.nextInt();
    System.out.println("请输入状态: ");
    String state = scanner.nextLine();
    House newhouse=new House(0,name,phone,address,rent,state);//把从前端接收的数据返回给后端
    if(houseService.add(newhouse)){
        System.out.println("添加信息成功！！");
    }
    else {
        System.out.println("添加信息失败");
    }
}
//实现的是删除房屋 凭借的是房屋的id
public void deleteHouse(){
    System.out.println("================删除房屋的信息==============");
    System.out.println("请输入你要删除房屋的编号(-1表示不删除)是什么");
    int deleteId=scanner.nextInt();
    if(deleteId==-1){
        System.out.println("你拒绝删除");
        return;
    }
    else{
        if(houseService.delete(deleteId)){//这里是交给后台去处理实现我们的流程
            System.out.println("删除成功");
        }
        else {
            System.out.println("删除失败");
        }
    }

}
public void read(){
    System.out.println("请输入你要查找的用户的id");
    int id = scanner.nextInt();
    if(houseService.read(id)){
        System.out.println("查找成功");
    }
    else{
        System.out.println("查找失败");
        return;
    }
}
public void updata(){

    System.out.println("请输入你要修改的成员的ID");
    int id = scanner.nextInt();
    if(houseService.find(id)){//这个是判断找的到情况下
        System.out.println("请输入姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入电话");
        String phone = scanner.nextLine();
        System.out.println("请输入地址:");
        String address = scanner.nextLine();
        System.out.println("请输入每日的租金:");
        int rent = scanner.nextInt();
        System.out.println("请输入状态: ");
        String state = scanner.nextLine();
        House newhouse = new House(id,name,phone,address,rent,state);
        if(houseService.Modify(newhouse,id)){
            System.out.println("修改成功");
        }
        else {
            System.out.println("修改失败");
        }

    }


}
    public void mainMenu(){
        do{
            System.out.println("=================房屋出租系统菜单===============");
            System.out.println("\t\t\t 1.新 增 房 源");
            System.out.println("\t\t\t 2查 找 房 屋");
            System.out.println("\t\t\t 3删 除 房 屋");
            System.out.println("\t\t\t 4修 改  房 屋");
            System.out.println("\t\t\t 5显 示  房 屋");
            System.out.println("\t\t\t 6退 出 房 屋");
            System.out.println("请输入你的选择");
            key=scanner.nextLine();

            switch (key){
                case "1":
//                    System.out.println("新增房源");
                    addHouse();
                    break;
                case "2":
                    read();
                    System.out.println("查找房源");
                    break;
                case "3":
                    deleteHouse();
                    System.out.println("删除房源");
                    break;
                case "4":
                      updata();
                    System.out.println("修改房源");
                    break;
                case "5":
                    listHouse();
                    System.out.println("显示房源");
                    break;
                case "6":
                    System.out.println("推出成功");
                    loop=false;
                    break;
            }

        }while (loop);

    }
}
